<?php

namespace App\Http\Controllers\Admin;

use App\Http\Filters\HousingsOrderFilter;
use App\Models\HousingsOrder;
use App\Http\Requests\HousingsOrderRequest;
use App\Http\Resources\HousingsOrderResource;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\HousingsOrderExport;

class HousingsOrderController extends Controller
{
    public function index(HousingsOrderFilter $filter)
    {
        $housingsOrders = HousingsOrder::query()->with('user','housings')
            ->filter($filter)
            ->paginate();

        return $this->ok(HousingsOrderResource::collection($housingsOrders));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(HousingsOrderRequest $request)
    {
        $inputs = $request->validated();
        $housingsOrder = HousingsOrder::create($inputs);

        return $this->created(HousingsOrderResource::make($housingsOrder));
    }

    public function edit(Request $request, HousingsOrder $housingsOrder)
    {
        return $this->ok(HousingsOrderResource::make($housingsOrder));
    }

    public function update(HousingsOrderRequest $request, HousingsOrder $housingsOrder)
    {
        $inputs = $request->validated();
        $housingsOrder->update($inputs);

        return $this->created(HousingsOrderResource::make($housingsOrder));
    }

    public function destroy(HousingsOrder $housingsOrder)
    {
        $housingsOrder->delete();
        return $this->noContent();
    }
    public function export(Request $request)
    {

        $filters = $request->only([
            'ids',
            // ... 其他可能的过滤条件
        ]);
        return Excel::download(new HousingsOrderExport($filters), '排房订单.xlsx');
    }
}
